﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using BusinessRule.System;

public partial class ThirdParty_Compnies_Item : PageBase
{
    private int id = 0;
    private Jumpton.Persistent.ObjectTable ot;
    private const string COM_ID = "id";
    private const string RESPONSE_FORM_LIST = "ThirdParty_Compnies_List.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[COM_ID] != null)
        {
            int.TryParse(Request.QueryString[COM_ID], out id);
        }

        if (!IsPostBack)
        {
            FillForm();
        }
    }

    private void FillForm()
    {
        if (id > 0)
        {
            ThirdPartyCompaniesManager manager = new ThirdPartyCompaniesManager();
            THIRDPARTY_COMPANIES fm = manager.Load(id);
            txtCode.Text = fm.CODE;
            txtName.Text = fm.NAME;
            txtAdd.Text = fm.ADDRESS;
            txtContact.Text = fm.CONTACT;
            txtMobile.Text = fm.MOBILE;
            txtTel.Text = fm.TEL;
            cbActive.Checked = fm.ACTIVE;
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            try
            {
                THIRDPARTY_COMPANIES tpc = null;
                ThirdPartyCompaniesManager tpcManager = new ThirdPartyCompaniesManager();
                if (id == 0)
                {
                    tpc = new THIRDPARTY_COMPANIES();
                    tpc.CODE = txtCode.Text.Trim();
                    tpc.NAME = txtName.Text.Trim();
                    tpc.CREATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                    tpc.ACTIVE = true;  
                }
                else
                {
                    tpc = tpcManager.Load(id);
                    tpc.ACTIVE = cbActive.Checked;
                }

                tpc.DOMAIN_ID = MyStateManager.DomainID;
                tpc.NAME = txtName.Text.Trim();
                tpc.ADDRESS = txtAdd.Text;
                tpc.CONTACT = txtContact.Text;
                tpc.TEL = txtTel.Text;
                tpc.MOBILE = txtMobile.Text;
                tpc.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                tpc.LAST_UPDATER = MyStateManager.UserID.ToString();

                string LogContent = "";

                if (tpc.State == DataRowState.Added)
                {
                    LogContent += "增加新的第三方公司" + txtCode.Text.Trim() + "，它的信息是：";
                }
                else
                {
                    LogContent += tpc.CODE + "信息发生变动，变动后信息是：";
                }
                LogContent += "系列代码：" + tpc.CODE + "，";
                LogContent += "系列名称：" + tpc.NAME + "，";

                bool Successful = tpcManager.Save(tpc);

                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                 Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
                 this.MyStateManager.UserID, LogContent);

                ShowMessage("保存成功", RESPONSE_FORM_LIST);
            }
            catch (Exception ex)
            {
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                      Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                      this.MyStateManager.UserID, "第三方公司 - 异常:" + ex.Message);
                
            }
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }

   

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(RESPONSE_FORM_LIST);
    }

}
